package com.youthburin.rmp.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.youthburin.base.controller.BaseController;
import com.youthburin.base.services.OpLogService;
import com.youthburin.base.util.DwzResponse;
import com.youthburin.base.util.DwzResponseUtils;
import com.youthburin.rmp.enums.RmpOpLogBusiTypeEnum;
import com.youthburin.rmp.enums.RmpOpLogTypeEnum;
import com.youthburin.rmp.util.CacheDesc;

@Controller
public class CacheController extends BaseController {
	@Autowired
	private CacheManager cacheManager;
	@Autowired
	@Qualifier("rmpOpLogService")
	private OpLogService opLogService;

	@RequiresPermissions("cache:list")
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/system/cache/show", method = RequestMethod.GET)
	public String showCache(ModelMap model) {
		String[] cacheNames = cacheManager.getCacheNames();
		if (cacheNames != null && cacheNames.length > 0) {
			List<CacheDesc> cacheList = new ArrayList<CacheDesc>(cacheNames.length);
			for (String cacheName : cacheNames) {
				Cache cache = cacheManager.getCache(cacheName);
				CacheDesc cacheDesc = new CacheDesc();
				cacheDesc.setName(cacheName);
				cacheDesc.setState(cache.getStatus().toString());
				cacheDesc.setDiskStoreSize(cache.getDiskStoreSize());
				cacheDesc.setMemoryStoreSize(cache.getMemoryStoreSize());
				cacheDesc.setMaxElementsOnDisk(cache.getCacheConfiguration().getMaxElementsOnDisk());
				cacheDesc.setMaxElementsInMemory(cache.getCacheConfiguration().getMaxElementsInMemory());

				cacheList.add(cacheDesc);
			}

			Comparator nameCodeComparable = new BeanComparator("name");
			Collections.sort(cacheList, nameCodeComparable);

			opLogService.addLog(RmpOpLogTypeEnum.LIST.toString(), RmpOpLogBusiTypeEnum.RMP_CACEH.toString());

			model.put("cacheList", cacheList);
		}

		return "/system/cache/show";
	}

	@RequiresPermissions("cache:clear")
	@RequestMapping(value = "/system/cache/clear/{cacheName}/")
	@ResponseBody
	public DwzResponse clearCache(@PathVariable("cacheName") String cacheName) {
		cacheManager.getCache(cacheName).removeAll();
		opLogService.addLog(RmpOpLogTypeEnum.DELETE.toString(), RmpOpLogBusiTypeEnum.RMP_CACEH.toString(), cacheName);
		return DwzResponseUtils.createSuccessResponse(false, "cache_panel");
	}

	@RequiresPermissions("cache:flush")
	@RequestMapping(value = "/system/cache/flush/{cacheName}/")
	@ResponseBody
	public DwzResponse flushCache(@PathVariable("cacheName") String cacheName) {
		cacheManager.getCache(cacheName).flush();
		opLogService.addLog(RmpOpLogTypeEnum.DELETE.toString(), RmpOpLogBusiTypeEnum.RMP_CACEH.toString(), cacheName);
		return DwzResponseUtils.createSuccessResponse(false, "cache_panel");
	}
}
